package com.itheima.health.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.utils.SMSUtils;
import com.itheima.health.utils.ValidateCodeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 验证码模块
 *
 * @author HuChunping
 * @date 2021/01/16
 **/
@RestController
@RequestMapping("/validateCode")
public class ValidateController {

    private static final Logger log = LoggerFactory.getLogger(ValidateController.class);

    @Autowired
    private JedisPool jedisPool;

    /**
     * @Description: 发送验证码
     * @Author: HuChunPing
     * @Date: 2021/1/16 21:29
     * @param telephone:
     * @return: com.itheima.health.entity.Result
     **/
    @PostMapping("/send4Order")
    public Result send4Order(String telephone){
        // 从jedis池中获取一个连接
        Jedis jedis = jedisPool.getResource();
        // 创建key，并标记这个是订单(预约)的key(与手机号拼接，保证手机唯一)
        String key = RedisMessageConstant.SENDTYPE_ORDER + "_" + telephone;
        if (jedis.exists(key)) {
            return new Result(false, "验证码已经发送过了，请注意查收");
        }
        // 不存在
        // 生成验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        log.debug("=============生成验证码:{}",validateCode);
        // 发送验证码
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone, validateCode+"");
        } catch (ClientException e) {
            log.debug("验证码发送失败：{}:{}",telephone,validateCode);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        // 成功发送验证码
        // 将验证码存入redis，设置过期时间为10分钟
        jedis.setex(key, 10*60, validateCode+"");
        jedis.close();
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     * 手机号登录
     * @Author: HuChunping
     * @Date: 2021/1/17 22:44
     * @param telephone:
     * @return: com.itheima.health.entity.Result
     **/
    @PostMapping("/send4Login")
    public Result send4Login(String telephone){
        // 从jedis池中获取一个连接
        Jedis jedis = jedisPool.getResource();
        // 创建key，并标记这个是订单(预约)的key(与手机号拼接，保证手机唯一)
        String key = RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
        if (jedis.exists(key)) {
            return new Result(false, "验证码已经发送过了，请注意查收");
        }
        // 不存在
        // 生成验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        log.debug("=============生成验证码:{}",validateCode);
        // 发送验证码
        try {
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone, validateCode+"");
        } catch (ClientException e) {
            log.debug("验证码发送失败：{}:{}",telephone,validateCode);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        // 成功发送验证码
        // 将验证码存入redis，设置过期时间为10分钟
        jedis.setex(key, 10*60, validateCode+"");
        jedis.del(key);
        jedis.close();
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
